<?php
namespace App\Repository;

use App\Model\UserOut;
use App\Facade\Event;
use App\Event\UserOut\StatusNoEvent;

class UserOutRepository extends \HyperfStore\Repository
{
    // 放入模型
    public function build()
    {
        return new UserOut;
    }

    // 创建
    public function create(UserRepository $user, $data)
    {
        $this->model->user()->associate($user->model());

        return $this->exception(function() use($data) {
            return $this->save($data);
        });
    }
    
    // 同意
    public function setStatusOk()
    {
        if($this->statusPost())
        {
            $this->model->status = UserOut::STATUS_OK;
            
            return $this->model->save();
        }
    }
    
    // 拒绝
    public function setStatusNo()
    {
        if($this->statusPost())
        {
            Event::dispatch(new StatusNoEvent($this));
            
            $this->model->status = UserOut::STATUS_NO;
            
            return $this->model->save();
        }
    }
    
    // 是否可操作
    public function statusPost()
    {
        return $this->model->status == UserOut::STATUS_POST;
    }
}